package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse order(long userId, Integer page, Integer size) {

        PageHelper.startPage(page, size);
        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("userId",userId);
        List<Order> orders = orderMapper.selectByExample(example);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        //将订单商品信息和地址信息填充完整
        List<OrderDetailInfo> orderDetailInfos = orderConverter.order2detail(orders);
        for (OrderDetailInfo orderDetailInfo : orderDetailInfos) {
            setOrderItems(orderDetailInfo);
            setOrderShipping(orderDetailInfo);
        }

        System.out.println(orderDetailInfos);
        //返回orderlistResponse
        OrderListResponse orderListResponse = new OrderListResponse();
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal(pageInfo.getTotal());
        return orderListResponse;
    }


    /**
     * 查找订单地址信息并赋值给OrderDetailResponse
     * @param orderDetailInfo
     */
    private void setOrderShipping(OrderDetailInfo orderDetailInfo) {
        String orderId = orderDetailInfo.getOrderId();
        Example example = new Example(OrderShipping.class);
        example.createCriteria().andEqualTo("orderId",orderId);
        List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(example);
        OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShippings.get(0));
        orderDetailInfo.setOrderShippingDto(orderShippingDto);


    }


    /**
     * 查找订单商品信息并赋值给OrderDetailResponse
     * @param orderDetailInfo
     */
    private void setOrderItems(OrderDetailInfo orderDetailInfo) {
        String orderId = orderDetailInfo.getOrderId();
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderId",orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        List<OrderItemDto> orderItemDto = orderConverter.item2dto(orderItems);
        orderDetailInfo.setOrderItemDto(orderItemDto);
    }

    /**
     * 查询订单详情
     * @param request
     * @return
     */
    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {

        OrderDetailResponse orderDetailResponse = new OrderDetailResponse();

        try{
            //查询订单详情
            Order order = new Order();
            order.setOrderId(request.getOrderId());
            Order orderDetail = orderMapper.selectByPrimaryKey(order);
            //查询订单商品列表
            Example orderItemExample = new Example(OrderItem.class);
            orderItemExample.createCriteria().andEqualTo("orderId",order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            //查询订单地址信息
            Example orderShippingExample = new Example(OrderShipping.class);
            orderShippingExample.createCriteria().andEqualTo("orderId",order.getOrderId());
            List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(orderShippingExample);
            OrderShipping orderShipping = orderShippings.get(0);

            orderDetailResponse.setOrderTotal(orderDetail.getPayment());
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailResponse.setGoodsList(orderItemDtos);
            orderDetailResponse.setTel(orderShipping.getReceiverPhone());
            orderDetailResponse.setOrderStatus(orderDetail.getStatus());
            orderDetailResponse.setStreetName(orderShipping.getReceiverAddress());
            orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            log.error("OrderQueryServiceImpl.orderDetail occur Exception :" +e);
            ExceptionProcessorUtils.wrapperHandlerException(orderDetailResponse,e);
        }
        return orderDetailResponse;
    }
}
